package cn.hsw.component.generator;

import cn.hsw.component.data.base.BaseModel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * mybatis 代码生成器
 * @author 李子木
 */
public class MybatisGenerator {

    public static void main(String[] args) {

        /*权限*/
        generator("cn.hsw.manage","permission","hsw_manage_");
        /*角色*/
        generator("cn.hsw.manage","role","hsw_manage_");
        /*用户*/
        generator("cn.hsw.manage","user","hsw_manage_");
    }

    private static void generator(String basePackage,String module,String prefix){
        /*要生成的表*/
        List<String> tables = new ArrayList<>();
        tables.add(prefix+module);

        FastAutoGenerator.create("jdbc:mariadb:failover://119.45.151.110:3306/hsw-savings","root","hsw@2020")
                .globalConfig(builder -> {
                    builder.author("李子木")               //作者
                            .outputDir(System.getProperty("user.dir")+"\\src\\main\\resources\\temp\\")    //输出路径
                            .enableSwagger()           //开启swagger
                            .commentDate("yyyy-MM-dd HH:mm:ss")
                            .fileOverride();            //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent(basePackage)
                            .moduleName(module)
                            .mapper("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml,System.getProperty("user.dir")+"\\src\\main\\resources\\temp\\"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix(prefix)
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .enableLombok()
                            .superClass(BaseModel.class)
                            .addSuperEntityColumns("pid","create_time","last_update_time","create_by","update_by","remark")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
